package cn.cloudreal.cbms.system.action;

import java.util.List;

import org.apache.log4j.Logger;

import cn.cloudreal.cbms.common.util.Constants;
import cn.cloudreal.cbms.common.util.PageBean;
import cn.cloudreal.cbms.system.dao.impl.SampleDaoImpl;
import cn.cloudreal.cbms.system.service.SampleService;
import cn.cloudreal.cbms.vo.Sample;

import com.opensymphony.xwork2.ActionSupport;

/**
 * <p>
 * Title: ITC
 * </p>
 * <p>
 * Description:查询数据服务层视图类
 * </p>
 * <p>
 * Copyright: Copyright (c) 2010
 * </p>
 * <p>
 * Company:云恒瑞通
 * </p>
 * 
 * @author chendanggui
 * @date 2011-11-02
 */
public class SampleAction extends ActionSupport
{
    /** serialVersionUID */
    private static final long serialVersionUID = 1L;

    /** 日志工具类实例 */
    private static final Logger log = Logger.getLogger(SampleDaoImpl.class);

    /** id */
    long id;

    /** 服务 */
    SampleService sampleService;

    /** 数据 */
    List<Sample> samples;

    /** 数据 */
    Sample sample;

    /** 当前页 */
    int currentPage;

    /** 分页对象 */
    PageBean page;

    /** falg：1：查看，2：修改初始化，3：修改确认 ，4：新增保存 */
    String flag;

    /**
     * 列表
     * 
     * @return String
     */
    public String list()
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter SampleAction list() method,return String");
        }
        try
        {
            // 实例化 page分页对象
            page = new PageBean(currentPage == 0 ? 1 : currentPage,
                    Constants.PAGE_SIZE);

            samples = sampleService.getSamples(page);

        }
        catch (Exception e)
        {
            // TODO: handle exception 添加异常信息 返回error
            log.error("Error SampleAction list() method,return String", e);
            return ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit SampleAction list() method,return String");
        }
        return SUCCESS;
    }

    /**
     * 查看
     * 
     * @return
     */
    public String view()
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter SampleAction view() method,return String");
        }
        try
        {
            sample = sampleService.getSample(id);

            if (Constants.IS_VIEW.equals(flag))
            {
                // 打印退出方法体日志
                if (log.isInfoEnabled())
                {
                    log.info("Exit SampleAction view() method,return String view");
                }
                return "view";
            }
            else
            {
                // 打印退出方法体日志
                if (log.isInfoEnabled())
                {
                    log.info("Exit SampleAction view() method,return String edit");
                }
                return "edit";
            }
        }
        catch (Exception e)
        {
            // TODO: handle exception 添加异常日志
            log.error("Error SampleAction view() method,return String edit", e);
            return ERROR;
        }

    }

    /**
     * 删除
     * 
     * @return
     */
    public String delete()
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter SampleAction delete() method,return String");
        }

        try
        {
            // 调用service 方法执行删除操作
            sampleService.delSample(id);
        }
        catch (Exception e)
        {
            // TODO: handle exception 添加异常日志 返回Error
            log.error("Error SampleAction delete() method,return String", e);
            return ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit SampleAction delete() method,return String ");
        }
        return SUCCESS;
    }

    /**
     * 保存
     * 
     * @return
     */
    public String save()
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter SampleAction save() method,return String");
        }

        try
        {
            // 如果是添加操作则调用service 执行添加操作
            if (Constants.IS_ADD.equals(flag))
            {
                sampleService.addSample(sample);
            }
            else if (Constants.IS_UPDATE.equals(flag))
            {
                sampleService.updateSample(sample);
            }

        }
        catch (Exception e)
        {
            // TODO: handle exception
            // 打印方法体异常日志
            log.error("Error SampleAction save() method,return String ", e);
            return ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit SampleAction save() method,return String ");
        }
        return SUCCESS;
    }

    public SampleService getSampleService()
    {
        return sampleService;
    }

    public void setSampleService(SampleService sampleService)
    {
        this.sampleService = sampleService;
    }

    public List<Sample> getSamples()
    {
        return samples;
    }

    public void setSamples(List<Sample> samples)
    {
        this.samples = samples;
    }

    public Sample getSample()
    {
        return sample;
    }

    public void setSample(Sample sample)
    {
        this.sample = sample;
    }

    public int getCurrentPage()
    {
        return currentPage;
    }

    public void setCurrentPage(int currentPage)
    {
        this.currentPage = currentPage;
    }

    public long getId()
    {
        return id;
    }

    public void setId(long id)
    {
        this.id = id;
    }

    public String getFlag()
    {
        return flag;
    }

    public void setFlag(String flag)
    {
        this.flag = flag;
    }

    public PageBean getPage()
    {
        return page;
    }

    public void setPage(PageBean page)
    {
        this.page = page;
    }
}
